<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Baitap extends CI_Controller {

	public function index() {
		$this->load->model('model_trangchu');

		$data['baitap'] = $this->model_trangchu->laybaitap();

		$this->load->view("view_header");
		$this->load->view("view_login");
		$this->load->view("view_body_baitap",$data);
		$this->load->view("view_footer");
	}

	public function xemchitiet($mabaitap) {
		$this->load->model('model_trangchu');

		$baitap['baitap'] = $this->model_trangchu->GetBaiTap($mabaitap);

		if(($mahs = $this->session->userdata("prie_username"))&&($this->session->userdata("prie_phanloai") == "Học sinh")) {

			$baitap['malambai'] = $this->model_trangchu->GetMaLamBai($mahs, $mabaitap);
		}

		$this->load->view('view_header');
		$this->load->view('view_login');
		$this->load->view('view_xembaitap', $baitap);
		$this->load->view('view_footer');
	}

	public function lambai($mabaitap) {
		$this->load->model('model_trangchu');

		$baitap['baitap'] = $this->model_trangchu->noidungbaitap($mabaitap);
		$baitap['mabaitap'] = $mabaitap;
		$baitap['tenbaitap'] = $baitap['baitap'][0]->tenbaitap;

		$this->load->view('view_header');
		$this->load->view('view_login');
		$this->load->view('view_baitap', $baitap);
		$this->load->view('view_footer');
	}

	public function nopbai() {

		$this->load->model("model_trangchu");

		$mabt = $this->input->post("mabt");

		$socauhoi = $this->model_trangchu->CountCauHoi($mabt);

		$traloi = array();

		for ($i=0; $i<$socauhoi; $i++) {
			$cau = $i + 1;
			$traloi[$i] = $this->input->post("cau$cau");
		}

		$baitap = $this->model_trangchu->GetDapAn($mabt);

		$dapan = array();

		$i = 0;
		foreach ($baitap as $key) {
			$dapan[$i] = $key->dapan;
			$i++;
		}


		if (($this->session->userdata("prie_username"))&&($this->session->userdata("prie_phanloai") == "Học sinh")) {

			$mahs = $this->session->userdata("prie_username");

			$diem = 0;
			for ($j=0; $j<$i; $j++) {
				if ($traloi[$j] == $dapan[$j]) 
					$diem++;
			}

			$lambai = array(
				'mahocsinh' => $mahs,
				'mabaitap' => $mabt,
				'diem' => $diem.'/'.$i,
				'thoigianlam' => null,
				'giaoviennhanxet' => null
			);
			
			$this->model_trangchu->InsertLamBai($lambai);

			$malb = $this->model_trangchu->GetMaLamBai($mahs, $mabt)->malambai;

			$data = array();

			for ($i=0; $i<count($traloi); $i++) {

				$data[$i] = array(
					'malambai' => $malb,
					'cau' => $i+1,
					'dapan' => $dapan[$i],
					'traloi' => $traloi[$i]
				);
			}

			$this->model_trangchu->InsertChiTietLamBai($data);

		}
		

		$res = "";
		for ($i=0; $i<$socauhoi; $i++) {
			$res .= $dapan[$i] . "|";
		}

		print $res;
	}

	public function xemlai($mabt) {
		$this->load->model('model_trangchu');

		$mahs = $this->session->userdata['prie_username'];

		$malb = $this->model_trangchu->GetMaLamBai($mahs, $mabt)->malambai;

		$baitap['baitap'] = $this->model_trangchu->noidungbailam($malb);
		$baitap['mabaitap'] = $mabt;
		$baitap['tenbaitap'] = $baitap['baitap'][0]->tenbaitap;

		$this->load->view('view_header');
		$this->load->view('view_login');
		$this->load->view('view_xemlaibailam', $baitap);
		$this->load->view('view_footer');
	}
}